package com.lpit.service.impl;

import com.lpit.constant.Constant;
import com.lpit.dto.UserAndTableTheBehaviorDto;
import com.lpit.mapper.BrowsingHistoryMapper;
import com.lpit.result.ResponseResult;
import com.lpit.service.GiveAlikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 * @version 1.0.1
 * @apiNote 对文章的点赞行为
 */
@Service
public class GiveLikeServiceImpl implements GiveAlikeService {
    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;

    /**
     * 对文章的点赞行为
     * @param userAndTableTheBehaviorDto
     * @return ResponseResult<Integer>
     */
    @Override
    public ResponseResult<Integer> giveAlike(UserAndTableTheBehaviorDto userAndTableTheBehaviorDto) {
        /* 是否存在用户的id */
        if (null == userAndTableTheBehaviorDto.getUserId() || 0 == userAndTableTheBehaviorDto.getUserId()) {
            return new ResponseResult<>(404, null, Constant.THE_USER_WAS_NOT_FOUND);
        }
        /* 是否存在文章id*/
        if (null == userAndTableTheBehaviorDto.getArticleId() || 0 == userAndTableTheBehaviorDto.getArticleId()) {
            return new ResponseResult<>(404, null, Constant.NO_RELATED_ARTICLE_FOUND);
        }
        /* 是否存在行为值 , 行为值是否违法*/
        if (null == userAndTableTheBehaviorDto.getArticleStatus()) {
            userAndTableTheBehaviorDto.setArticleStatus(Constant.DZ);
        }
        /* 是否存在时间信息*/
        if (null == userAndTableTheBehaviorDto.getDateTime()) {
            /* 创建当前的系统时间*/
            userAndTableTheBehaviorDto.setDateTime(new SimpleDateFormat(Constant.TIME_FORMAT).format(new Date()));
        }
        /* 向数据库插入数据*/
        Integer integer = browsingHistoryMapper.addGiveAlike(userAndTableTheBehaviorDto);
        if (null == integer || 0 == integer) {
            return new ResponseResult<>(404, null, Constant.MISTAKE);
        }
        return new ResponseResult<>(200, integer, Constant.SUCCESS);
    }
}
